package de.velvettear.squirrel.util;

import java.util.Set;
import java.util.Vector;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import de.velvettear.squirrel.kernel.Device;

public class BluetoothHelper {

	private static BluetoothAdapter adapter = null;
	
	public static void enable(Context context) {
		Log.e("enable()", "BEGIN");
		
		if (adapter == null) {
			adapter = BluetoothAdapter.getDefaultAdapter();
		}
		
		if (!adapter.isEnabled()) {
			adapter.enable();
		}
		
		Log.e("enable()", "END");
	}
	
	public static void disable(Context context) {
		Log.e("disable()", "BEGIN");
		
		if (adapter == null) {
			adapter = BluetoothAdapter.getDefaultAdapter();
		}
		
		if (adapter.isEnabled()) {
			adapter.disable();
		}
		
		Log.e("disable()", "END");
	}
	
	public static boolean isEnabled() {
		Log.e("isEnabled()", "BEGIN");
		
		if (adapter == null) {
			adapter = BluetoothAdapter.getDefaultAdapter();
		}
						
		Log.e("isEnabled()", "END");
		
		return adapter.isEnabled();
	}
	
	public static boolean isDisabled() {
		Log.e("isDisabled()", "BEGIN");
		
		if (adapter == null) {
			adapter = BluetoothAdapter.getDefaultAdapter();
		}
		
		boolean isDisabled = !adapter.isEnabled();
		
		Log.e("isDisabled()", "END");
		
		return isDisabled;
	}
	
	public static Vector<Device> getPairedDevices() {
		Log.e("getPairedDevices()", "BEGIN");
		Vector<Device> devices = new Vector<Device>();		
		
		if (adapter == null) {
			adapter = BluetoothAdapter.getDefaultAdapter();
		}
				
		Set<BluetoothDevice> pairedDevices = adapter.getBondedDevices();
		for (BluetoothDevice device : pairedDevices) {
			Device dev = new Device(device.getName(), device.getAddress(), device.getClass().getName());
			devices.add(dev);
		}
				
		Log.e("getPairedDevices()", "END");
		return devices;
	}
	
}
